//
//  CarFriendNewViewController.m
//  LoveCar
//
//  Created by dlios on 15/5/28.
//  Copyright (c) 2015年 薛森鸿. All rights reserved.
//

#import "CarFriendNewViewController.h"
#import "CarFriendCell.h"
#import "CarFriendModel.h"
#import "MJRefresh.h"
#import "MJRefreshHeaderView.h"
#import "MJRefreshFooterView.h"
#import "CarFriendViewController.h"

@interface CarFriendNewViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) NSMutableArray *array;
@end

@implementation CarFriendNewViewController
- (void)dealloc
{
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.array = [NSMutableArray array];
    self.pageCount = 121056;
    [self step];
    [self createTable];
    [self setupRefresh];
}
// 设置
- (void)step
{
    self.navigationItem.hidesBackButton = YES;
    
    self.navigationItem.title = @"推荐";
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.translucent = NO;
    UIView *title = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
    [self.navigationItem setTitleView:title];
    [title release];
    
    UIButton *a = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
    [a setTitle:@"推荐" forState:UIControlStateNormal];
    [a setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    a.titleLabel.font = [UIFont systemFontOfSize:14];
    [a addTarget:self action:@selector(good) forControlEvents:UIControlEventTouchUpInside];
    [title addSubview:a];
    
    UIButton *b = [[UIButton alloc] initWithFrame:CGRectMake(80, 0, 70, 30)];
    [b setTitle:@"最新" forState:UIControlStateNormal];
    [b setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [title addSubview:b];
    
    UIBarButtonItem *edit = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"edit.png"] style:UIBarButtonItemStylePlain target:self action:@selector(editAction)];
    self.navigationItem.rightBarButtonItem = edit;
}

- (void)good
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

// 更新cell
- (void)reloadDataForCell
{
    NSString *str = @"http://api.ycapp.yiche.com/appforum/cheyouhome/?isall=1&deviceid=868033012409837";
    AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    [m GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:   NSJSONReadingMutableContainers error:nil];
        [self.array addObjectsFromArray:[dic objectForKey:@"data"]];
        [self.table reloadData];
        [self.table headerEndRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
/**
 *  集成刷新控件
 */
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.table addHeaderWithTarget:self action:@selector(headerRereshing)];
    //#warning 自动刷新(一进入程序就下拉刷新)
    [self.table headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.table addFooterWithTarget:self action:@selector(footerRereshing)];
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    [self reloadDataForCell];
}

- (void)footerRereshing
{
    NSString *str = [NSString stringWithFormat:@"http://api.ycapp.yiche.com/appforum/cheyouhome/?minid=%ld&isall=1&deviceid=868033012409837", self.pageCount];
    AFHTTPRequestOperationManager *m = [AFHTTPRequestOperationManager manager];
    m.responseSerializer = [AFHTTPResponseSerializer serializer];
    [m GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:   NSJSONReadingMutableContainers error:nil];
        [self.array addObjectsFromArray:[dic objectForKey:@"data"]];
        self.pageCount -= 100;
        [self.table reloadData];
        [self.table footerEndRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}
// 创建table
- (void)createTable
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) style:UITableViewStylePlain];
    _table.rowHeight = 700;
    _table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _table.dataSource = self;
    _table.delegate = self;
    UINib *nib = [UINib nibWithNibName:@"CarFriendCell" bundle:[NSBundle mainBundle]];
    [_table registerNib:nib forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_table];
    [_table release];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [self.array objectAtIndex:indexPath.row];
    NSDictionary *f = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    CGRect rect = [[dic objectForKey:@"context"] boundingRectWithSize:CGSizeMake(260, 90) options:NSStringDrawingUsesLineFragmentOrigin attributes:f context:nil];
    NSArray *imgList = [dic objectForKey:@"imageList"];
    CGFloat imgHeight = (imgList.count + 2) / 3 * 85;
    
    return 55 + rect.size.height + imgHeight + 250;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CarFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSDictionary *dic = [self.array objectAtIndex:indexPath.row];
    [cell sampleCellWithDic:dic];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
